package com.studentSystem.controller;

import com.studentSystem.pojo.Student;
import com.studentSystem.service.StudentService;
import com.studentSystem.utils.PageBeanUtil;
import com.studentSystem.utils.ResultUtil;
import com.studentSystem.utils.SnowflakeIdWorkerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/student")
@Validated
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping
    public ResultUtil<PageBeanUtil<Student>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Long classId,
            @RequestParam(required = false) Long id
    ) {
        PageBeanUtil<Student> pb = studentService.list(pageNum, pageSize, classId, id);
        return ResultUtil.success(pb);
    }

    @DeleteMapping("/delete")
    public ResultUtil delete(Long id) {
        studentService.deleteById(id);
        return ResultUtil.success();
    }

    @PostMapping("/add")
    public ResultUtil add(@RequestBody @Validated Student student) {
        Long id = new SnowflakeIdWorkerUtil(0, 0).nextId();
        student.setId(id);
        studentService.add(student);
        return ResultUtil.success();
    }

    @PutMapping("/update")
    public ResultUtil update(@RequestBody @Validated Student student) {
        studentService.update(student);
        return ResultUtil.success();
    }

    @GetMapping("/detail")
    public ResultUtil<Student> detail(Long id) {
        Student student = studentService.getById(id);
        return ResultUtil.success(student);
    }
}
